package Haak::C::Code;

use strict;
use warnings;
use base qw( Haak );
use WWW::Mechanize;
use POE::Component::IKC::ClientLite;
use DateTime;
sub do_default {
    my $self = shift;
    my $nick = $self->req->param('nick');
    $self->stash->{nick} = $nick;
}

sub do_view {
    my $self = shift;

    my $rs = $self->M('MyDB::Codepad')->search;

    $self->stash->{rs} = $rs;
}

sub do_result {
    my $self = shift;

    my $code = $self->req->param('code');
    
    my $dt = DateTime->now( time_zone => 'Asia/Tokyo' );
    my $mech = WWW::Mechanize->new;

    $mech->get($self->config->{url}->{codepad});

    $mech->form_number(1);
    $mech->field('code'=> $code );
    $mech->field('lang'=>'Perl');
    if ($self->req->param('submit')) {
        $mech->click_button( name=>'submit', value=> 'Paste and Run');
    } else {
        $mech->click_button( name=> 'submit', value => 'Just Paste' );
    }

    eval {
	$self->M('MyDB::Codepad')->create({
	    nick      => $self->req->param('nick') || 'Unknown',
	    url       => $mech->uri,
	    create_at => $dt->ymd." ".$dt->hms,
					  });
    };

    $self->update($mech->uri);
    $self->res->redirect($mech->uri);
}

sub update {
    my ($self, $page) = @_;

    my $name = "Client$$";
    my $remote = create_ikc_client(
        port => 31338,
        name => $name,
        timeout => 1,
     );
    return unless $remote;
    my $return_value = $remote->post_respond( 'application/update', $page );
    return unless defined $return_value;
}

1;

=head1 NAME

Haak::C::Code - Soozy Component

=head1 SYNOPSIS

See L<Haak>

=head1 DESCRIPTION

Soozy Component

=head1 AUTHOR

JEEN

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut
